package cn.autumnorange.app.user.consumer.controller;

import cn.autumnorange.app.common.dto.ResultDto;
import cn.autumnorange.app.user.api.AppUserService;
import cn.autumnorange.app.user.api.dto.UserDetailDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class UserController implements AppUserService {
  @Autowired AppUserService appUserService;

  @Override
  public ResponseEntity<ResultDto<UserDetailDto>> loadUserByUsername(String name) {
    return null;
  }

  @Override
  public ResponseEntity<ResultDto<UserDetailDto>> loginSuccess() {

    Authentication authorization = SecurityContextHolder.getContext().getAuthentication();
    if (authorization != null) {
      Object obj = authorization.getPrincipal();
      if (obj instanceof UserDetails) {
        UserDetails userDetails = (UserDetails) authorization.getPrincipal();
        log.info(userDetails.toString());
      }
    }

    return appUserService.loginSuccess();
  }
}
